layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	// 渲染表格
	table.render({
		elem: '#partner_list',
		id: 'partner_list',
		url: partner_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'partner_id',
			_b: 'desc'
		},
		cols: [cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		initSort: {
			field: 'partner_id',
			type: 'desc'
		}
	});

	// 排序
	table.on('sort(partner_list)', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload('partner_list', {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	// 表单input 查询
	$('form[lay-filter="partner_search_form"]').on('input propertychange', 'input[type="text"]', function() {
		if($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload('partner_list', {
			where: where
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart',false);
		$(this).trigger('input');
	});
	form.render();
	// 时间选择器
	laydate.render({
		elem: 'input[name="start_time"]',
		range: true,
		done: function(value, date, endDate) {
			var value = value;
			var name = 'start_time';
			var where = {};
			where[name] = value;
			table.reload('partner_list', {
				where: where
			});
		}
	});
	// 搜索是否买断
	form.on('radio(maiduan)', function(data) {
		var value = data.value;
		var name = 'maiduan';
		var where = {};
		where[name] = value
		table.reload('partner_list', {
			where: where
		})
	})
	// 时间选择器
	laydate.render({
		elem: 'input[name="end_time"]',
		range: true,
		done: function(value, date, endDate) {
			var value = value;
			var name = 'end_time';
			var where = {};
			where[name] = value;
			table.reload('partner_list', {
				where: where
			});
		}
	})
	// 顶部删除按钮
	$('.del-partner').on('click', function() {
		var checkStatus = table.checkStatus('partner_list');
		var tr = checkStatus.tr;
		if(checkStatus.data.length == 0) {
			layer.msg('请选择要删除的数据', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		var partner_id_arr = new Array();
		layer.confirm('确定删除选中数据吗？', function(index) {
			checkStatus.data.map(function(obj, i) {
				partner_id_arr.push(obj.partner_id);
			});
			$.post(partner_delete_url, {
				partner_id: partner_id_arr
			}, function(data) {
				if(data.code == 0) {
					tr.map(function(obj, i) {
						$(obj.selector).remove();
					})
					layer.msg('删除成功', {
						icon: 1
					});
					layer.close();
				} else {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				}
			}, 'json')
		})
	})
	// 表格工具监听
	table.on('tool(partner_list)', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var partner_id = data.partner_id;
		switch(e) {
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					$.post(partner_delete_url, {
						partner_id: partner_id
					}, function(data) {
						if(data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					}, 'json')
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				$.post(partner_one_url,{partner_id:data.partner_id},function(data){
					delete data.data.partner_id;
					obj.update(data.data);
				},'json')
				break;
			default:
				break;
		}
	})
})